package servlet;

import bean.Goods;
import bean.Cart;
import service.impl.CartServiceImpl;
import utils.CreateBeanUtils;
import utils.DataTypeUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

public class CartServlet extends BaseServlet {

    CartServiceImpl cartService = new CartServiceImpl();

    /**
     * 登录账号添加商品到购物车
     */
    protected void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String bookId = req.getParameter("bookId");
        String bookName = req.getParameter("bookName");
        String bookPrice = req.getParameter("bookPrice");

        Goods goods = new Goods();

        goods.setId(DataTypeUtils.parseInt(bookId, -1));
        goods.setName(bookName);
        goods.setPrice(new BigDecimal(bookPrice));

//        System.out.println(CartServlet.class.getName() + "  ---->  " + book);

        Number number = (Number) req.getSession().getAttribute("userId");


        cartService.addbook(goods, number.intValue());

        resp.sendRedirect(req.getHeader("Referer"));

    }


    /**
     * 获取登录账号的购物车中的所有信息
     */
    protected void getCartInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        pages/cart/cart.jsp
        Number number = (Number) req.getSession().getAttribute("userId");
        Cart cartInfo = cartService.getCartInfo(number.intValue());

        req.setAttribute("cartInfo",cartInfo);

        //在结算失败后，用于显示库存不足的图书
        Object stockOut = req.getSession().getAttribute("stockOut");
        if(stockOut != null){
            req.setAttribute("stockOut",stockOut);
            req.getSession().removeAttribute("stockOut");
        }

        req.getRequestDispatcher("/pages/cart/cart.jsp").forward(req,resp);

    }

    /**
     * 更新购物车图书的数量
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String count = req.getParameter("count");

        int countInt = DataTypeUtils.parseInt(count, 1);

        String itemId = req.getParameter("itemId");

        int itemIdInt = DataTypeUtils.parseInt(itemId, -1);

        cartService.updateCount(itemIdInt,countInt);

        resp.sendRedirect(req.getContextPath() + "/cartServlet?action=getCartInfo");

    }

    /**
     * 删除购物车中的单个商品
     */
    protected void deletebook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        pages/cart/cart.jsp
        Number number = (Number) req.getSession().getAttribute("userId");
        String itemId = req.getParameter("itemId");

        int anInt = DataTypeUtils.parseInt(itemId, -1);

        cartService.deletebook(anInt,number.intValue());

        resp.sendRedirect(req.getContextPath() + "/cartServlet?action=getCartInfo");

    }

    /**
     * 清空购物车
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Number number = (Number) req.getSession().getAttribute("userId");
        cartService.clear(number.intValue());
        resp.sendRedirect(req.getContextPath() + "/cartServlet?action=getCartInfo");
    }

    /**
     * 在图书管理页面，修改图书信息后，同步到购物车中
     */
    protected void syncInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Goods goods = CreateBeanUtils.createBeanByParams(Goods.class, req.getParameterMap());

        System.out.println(CartServlet.class.getName() + "syncInfo() ----> " + goods);

        cartService.syncInfo(goods);

        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber=" +  req.getParameter("pageNumber"));

    }
}
